<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Provider's registration</title>
    <link rel="stylesheet" href="css/main.css" />
    <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
        <script type="text/javascript" src="xui.js"></script>
        <script type="text/javascript" src="jquery-1.5.min.js"></script>
        <script type="text/javascript" src="jquery.scrollTo-min.js"></script>
        <script type="text/javascript" src="templating.js"></script>
    </head>
<body>
	
    <div align="center" id="menu">
        <h1 align="center">Menu</h1>
        <p>
            <button id="callRegForm" class="buttn"> Registration</button>
        </p>
        <p>
            <button id="callAutForm" class="buttn">Authentication</button>
        </p>
    </div>
    
    <div id="regForm">
        <table>
			<tr>
                <td colspan=2 align="center"><h3>Fill registration fields</h3></td>
            </tr>
            <tr>
                <td>Name</td>
                <td><input id="name" type=text class="formInfo" value="Bob"/></td>
            </tr>
            
            <tr>
                <td>login</td>
                <td><input id="login" type=text class="formInfo" value="login"/></td>
            </tr>
            <tr>
                <td>lastname</td>
                <td><input id="lastname" type=text class="formInfo" value="Smith"/></td>
            </tr>
            <tr>
                <td>zip</td>
                <td><input id="zip" type=text class="formInfo" value="48015"/></td>
            </tr>
            <tr>
                <td>stateProvince</td>
                <td><input id="stateProvince" type=text class="formInfo" value="Massachusett"/></td>
            </tr>
            <tr>
                <td>city</td>
                <td><input id="city" type=text class="formInfo" value="Boston"/></td>
            </tr>
            <tr>
                <td>typeOfCar</td>
                <td><input id="typeOfCar" type=text class="formInfo" value="sedan"/></td>
            </tr>
            <tr>
                <td>seatsNumber</td>
                <td><input id="seatsNumber" type=text class="formInfo" value="4"/></td>
            </tr>
            <tr>
                <td>farePerMinute</td>
                <td><input id="farePerMinute" type=text class="formInfo" value="10"/></td>
            </tr>
            <tr>
                <td>fareFixed</td>
                <td><input id="fareFixed" type=text class="formInfo" value="10"/></td>
            </tr>
            <tr>
                <td>email</td>
                <td><input id="email" type=text class="formInfo" value="bob@hotmail.com"/></td>
            </tr>
            <tr>
                <td>country</td>
                <td><input id="country" type=text class="formInfo" value="USA"/></td>
            </tr>
            <tr>
                <td colspan=2 align="center" id="error" class="textErr"><br /></td>
            </tr>
            
            <tr>
                <td colspan=2 align="center"><button id="registration" class="buttn">Registretion</button></td>
            </tr>

		</table>
	</div>
    <div id="autForm">
        <table>
            <tr>
                <td> Login: </td>
                <td> <input id="aLogin" type="text"/> </td>
            </tr>
            <tr>
                <td>Password: </td>
                <td> <input id="pass" type="text"/> </td>
            </tr>
            <tr>
                <td colspan=2 align="center" id="aError" class="textErr"><br /></td>
            </tr>
            <tr>
                <td>
                    <button class="buttn" id="authentication">Authentication</button>
                </td>
            </tr>
        </table>
    </div>
     	
</body>
</html>
<script type="text/javascript" charset="utf-8">
    
    var pass;
    
    x$('#callRegForm').on('click',function(p){
        document.getElementById("menu").style.display = 'none';
        document.getElementById("regForm").style.display = 'inline';
    });
    x$('#callAutForm').on('click',function(p){
        document.getElementById("menu").style.display = 'none';
        document.getElementById("autForm").style.display = 'inline';
    });

    x$('#exit').on('click',function(p){
    	navigator.app.exitApp();
    });
    
    $('#registration').click(function start(){
    	
        document.getElementById('error').innerHTML = "";
    	var login = $("#login").val();
        var name = $("#name").val();
        var lastname = $("#lastname").val();
        var zip = $("#zip").val();
        var city = $("#city").val();
        var stateProvince = $("#stateProvince").val();
        var country = $("#country").val();
        var email = $("#email").val();
        var typeOfCar = $("#typeOfCar").val();
        var seatsNumber = $("#seatsNumber").val();
        var fareFixed = $("#fareFixed").val();
        var farePerMinute = $("#farePerMinute").val();
        

    	if(login != '')
    	{
            
    		$.ajax({
                    url: "http://10.0.2.2:9000/provider/registration",
                    data: {login:login, name:name, lastname:lastname,zip:zip, city:city, stateProvince:stateProvince,country:country,email:email,typeOfCar:typeOfCar,seatsNumber:seatsNumber,fareFixed:fareFixed,farePerMinute:farePerMinute},
                    success: function(event) {
                        if (event.error == 0)
                        {
                        	alert("Your password is " + event.pass);
                        	pass = event.pass;
                            $("#pass").val(event.pass);
                            $("#aLogin").val(login);
                            document.getElementById("menu").style.display = 'inline';
                            document.getElementById("regForm").style.display = 'none';
                        }
                        else
                        {
                        	document.getElementById('error').innerHTML = "Provider with this login alreay exist. Choose another login";
                        }
                    },
                    dataType: 'json'
                });
        	
    	}
    	else
    	{
    		document.getElementById('error').innerHTML = "Please enter login";
    	}

    	
    });
    
    $('#authentication').click(function start(){
    	document.getElementById('error').innerHTML = "";
    	var login = $('#login').val();
    	var password = $('#pass').val();
    	
    	if ((login != '') && (password != ''))
    	{
    		
    		$.ajax({
                    url: "http://10.0.2.2:9000/provider/authentication",
                    data: {login: login, password:password},
                    success: function(event) {
                        if (event == true)
                        {
                        	window.location="index.html";
                        }
                        else
                    	{
                        	document.getElementById('aError').innerHTML = "Wrong password or login";
                        }
                    },
                    dataType: 'json'
                });
        	
    		
    	}
    	else
    	{
    		document.getElementById('error').innerHTML = "Please enter name and password";
    	}
    });
    
    
</script>